$(function () { var userLang = navigator.language || navigator.userLanguage; recognition.lang = userLang; recognition.start(); }); if (!('webkitSpeechRecognition' in window)) { alert('You need to use the latest version of Google Chrome for this to work'); } else { var recognition = new webkitSpeechRecognition(); recognition.interimResults = true; recognition.continuous = true; recognition.onstart = function () { }; recognition.onend = function () { }; var originalText = ''; var beginningOfSentence = true; recognition.onresult = function (event) { if (beginningOfSentence) { originalText = $('#box2').val(); beginningOfSentence = false; } beginningOfSentence = false; let txt = ''; for (var i = event.resultIndex; i < event.results.length; i++) { txt += event.results[i][0].transcript; if (event.results[i].isFinal) { txt += ". "; beginningOfSentence = true; $('#social').fadeIn(5000); } } txt = originalText + capitalize(txt); $('#box2').val(txt); }; } function capitalize(str) { return str.replace(/\S/, function (str) { return str.toUpperCase(); }); } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }